﻿using System;
using System.Collections.Generic;
using System.Windows;

namespace JYYQ.BMS
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Battery battery = new Battery();

        private string[] delDataCommond = { "$CLS1 *", "$CLS*", "$CLS3 *", "$CLS4*", "$CLS5 *", "$CLS6*", "$CLS7 *", "$CLS8*", "$CLS9*", "$CLS10*" };

        private byte[] nonByteData = new byte[] { 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8 };

        private PortState portSate = PortState.ReadData;

        private string[] readDataCommond = { "$RQ1*", "$RQ2*", "$RQ3*", "$RQ4*", "$RQ5*", "$RQ6*", "$RQ7*", "$RQ8*", "$RQ9*", "$RQ10*" };

        private string readVoltageCommond = "$READV*";

        private string setVolatgeCommond = "$V*";

        public MainWindow()
        {
            InitializeComponent();
            InitCore();
        }

        private enum PortState
        { ReadData, ReadVoltage, SetValtage };

        private void BindData()
        {
            Random r = new Random();
            IList<ChargeData> list = new List<ChargeData>();
            for (int i = 0; i < 50; i++)
            {
                ChargeData data = new ChargeData();
                data.Hour = 3;
                data.Minute = i;
                data.Voltage = r.NextDouble() + r.Next(3, 5);
                data.Current = r.NextDouble() + r.Next(1, 5);
                list.Add(data);
            }
            this.battery.BData = list;
            this.battery.BType = BatteryType.铅酸蓄电池;
            this.battery.BStandVolage = 24;
            this.battery.BCapcity = 12;
            this.battery.BLevel = 2;
            this.battery.BTime = DateTime.Now;
            this.recvDataDataGrid.ItemsSource = this.battery.BData;
            this.recvDesTextBlock.DataContext = this.battery;
        }

        private void readRecvDataButton_Click(object sender, RoutedEventArgs e)
        {
            int index = this.readDataComboBox.SelectedIndex;
            portSate = PortState.ReadData;
            SerialPortWrite(readDataCommond[index]);
        }

        private void readVolatageButton_Click(object sender, RoutedEventArgs e)
        {
            portSate = PortState.ReadVoltage;
            SerialPortWrite(readVoltageCommond);
        }

        private void saveSerialDataMenuItem_Click(object sender, RoutedEventArgs e)
        {
        }

        private void setVolatageButton_Click(object sender, RoutedEventArgs e)
        {
            double vol;
            portSate = PortState.SetValtage;
            if (!double.TryParse(txtStandVoltage.Text.Trim(), out vol))
            {
                MessageBox.Show("基准电压的范围为5.00-5.70伏特，只能输入数字，请重新输入，谢谢！");
                return;
            }

            if (vol >= 5.00 && vol <= 5.70)
            {
                SerialPortWrite(setVolatgeCommond.Insert(2, vol.ToString("F2")));
            }
            else
            {
                MessageBox.Show("基准电压的范围为5.00-5.70伏特,请重新输入！");
            }
        }

        /// <summary>
        /// 只允许输入数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtStandVoltage_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            if (!char.IsDigit(e.Text, e.Text.Length - 1))
                e.Handled = true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            byte[] testData = new byte[]
            {
                  0X4A,0X02,0X00,0X78,0X00,0X58,0X02,0X00,0X00,0X00,0X81,0X00,0X00,0X00,0X00,0X01,0X90,0X00,0X08,0X00,0X00,0X02,0X90,0X00,0X07,0X00,0X00,0X03,0X90,0X00,0X06,0X00,0X00,0X04,0X90,0X00,0X07,0X00,0X00,0X05,0X90,0X00,0X06,0X00,0X00,0X06,0X90,0X00,0X06,0X00,0X00,0X07,0X90,0X00,0X06,0X00,0X00,0X08,0X90,0X00,0X06,0X00,0X00,0X09,0X90,0X00,0X06,0X00,0X00,0X0A,0X90,0X00,0X06,0X00,0X00,0X0B,0X90,0X00,0X06,0X00,0X00,0X0C,0X90,0X00,0X06,0X00,0X00,0X0D,0X90,0X00,0X06,0X00,0X00,0X0E,0X90,0X00,0X06,0X00,0X00,0X0F,0X90,0X00,0X06,0X00,0X00,0X10,0X90,0X00,0X06,0X00,0X00,0X11,0X90,0X00,0X06,0X00,0X00,0X12,0X90,0X00,0X06,0X00,0X00,0X13,0X90,0X00,0X06,0X00,0X00,0X14,0X90,0X00,0X06,0X00,0X00,0X15,0X90,0X00,0X06,0X00,0X00,0X16,0X90,0X00,0X06,0X00,0X00,0X17,0X90,0X00,0X06,0X00,0X00,0X18,0X90,0X00,0X06,0X00,0X00,0X19,0X8F,0X00,0X00,0X00,0X00,0X1A,0X8C,0X00,0X00,0X00,0X00,0X1B,0X89,0X00,0X00,0X00,0X00,0X1C,0X89,0X00,0X00,0X00,0X00,0X1D,0X87,0X00,0X00,0X00,0X00,0X1E,0X88,0X00,0X03,0X00,0X00,0X1F,0X89,0X00,0X04,0X00,0X00,0X20,0X89,0X00,0X04,0X00,0X00,0X21,0X89,0X00,0X04,0X00,0X00,0X22,0X89,0X00,0X04,0X00,0X00,0X23,0X89,0X00,0X04,0X00,0X00,0X24,0X89,0X00,0X04,0X00,0X00,0X25,0X89,0X00,0X04,0X00,0X00,0X26,0X89,0X00,0X04,0X00,0X00,0X27,0X89,0X00,0X04,0X00,0X00,0X28,0X89,0X00,0X04,0X00,0X00,0X29,0X89,0X00,0X04,0X00,0X00,0X2A,0X89,0X00,0X04,0X00,0X00,0X2B,0X89,0X00,0X04,0X00,0X00,0X2C,0X89,0X00,0X04,0X00,0X00,0X2D,0X89,0X00,0X04,0X00,0X00,0X2E,0X89,0X00,0X04,0X00,0X00,0X2F,0X89,0X00,0X03,0X00,0X00,0X30,0X89,0X00,0X04,0X00,0X00,0X31,0X89,0X00,0X04,0X00,0X00,0X32,0X89,0X00,0X04,0X00,0X00,0X33,0X89,0X00,0X04,0X00,0X00,0X34,0X89,0X00,0X04,0X00,0X00,0X35,0X89,0X00,0X04,0X00,0X00,0X36,0X89,0X00,0X04,0X00,0X00,0X37,0X89,0X00,0X04,0X00,0X00,0X38,0X89,0X00,0X04,0X00,0X00,0X39,0X89,0X00,0X04,0X00,0X00,0X3A,0X89,0X00,0X04,0X00,0X00,0X3B,0X89,0X00,0X04,0X00,0X01,0X00,0X89,0X00,0X04,0X00,0X01,0X01,0X89,0X00,0X04,0X00,0X01,0X02,0X89,0X00,0X04,0X00,0X01,0X03,0X89,0X00,0X04,0X00,0X01,0X04,0X89,0X00,0X04,0X00,0X01,0X05,0X89,0X00,0X04,0X00,0X01,0X06,0X89,0X00,0X04,0X00,0X01,0X07,0X89,0X00,0X04,0X00,0X01,0X08,0X89,0X00,0X04,0X00,0X01,0X09,0X89,0X00,0X04,0X00,0X01,0X0A,0X89,0X00,0X04,0X00,0X01,0X0B,0X89,0X00,0X04,0X00,0X01,0X0C,0X89,0X00,0X04,0X00,0X01,0X0D,0X89,0X00,0X04,0X00,0X01,0X0E,0X89,0X00,0X04,0X00,0X01,0X0F,0X89,0X00,0X02,0X00,0X01,0X10,0X89,0X00,0X04,0X00,0X01,0X11,0X89,0X00,0X04,0X00,0X01,0X12,0X89,0X00,0X04,0X00,0X01,0X13,0X89,0X00,0X04,0X00,0X01,0X14,0X89,0X00,0X04,0X00,0X01,0X15,0X89,0X00,0X04,0X00,0X01,0X16,0X89,0X00,0X04,0X00,0X01,0X17,0X89,0X00,0X02,0X00,0X01,0X18,0X89,0X00,0X04,0X00,0X01,0X19,0X89,0X00,0X04,0X00,0X01,0X1A,0X89,0X00,0X04,0X00,0X01,0X1B,0X89,0X00,0X04,0X00,0X01,0X1C,0X89,0X00,0X04,0X00,0X01,0X1D,0X89,0X00,0X04,0X00,0X01,0X1E,0X89,0X00,0X03,0X00,0X01,0X1F,0X89,0X00,0X04,0X00,0X01,0X20,0X89,0X00,0X04,0X00,0X01,0X21,0X89,0X00,0X04,0X00,0X01,0X22,0X89,0X00,0X04,0X00
            };
            //byte[] testData = nonByteData;
            BindData(testData);
        }
    }
}